package cn.djp.canting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import org.litepal.LitePal;

import java.util.List;

import cn.djp.canting.R;
import cn.djp.canting.adapter.SelectItemAdapter;
import cn.djp.canting.bean.AllFood;
import cn.djp.canting.bean.User;
import cn.djp.canting.bean.UserOrder;

//选择餐品
public class SelectActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar select_toolbar;
    private ListView select_listview;
    private final SelectActivity context = SelectActivity.this;
    private TextView select_allPrice;
    private Button select_finish;
    public int allallPrice;
    public String phone;
    private SelectItemAdapter selectItemAdapter;
    private List<AllFood> allFoods;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        initView();
    }

    private void initView() {
        select_toolbar = (Toolbar) findViewById(R.id.select_toolbar);
        select_listview = (ListView) findViewById(R.id.select_listview);
        select_allPrice = (TextView) findViewById(R.id.select_allPrice);
        select_finish = (Button) findViewById(R.id.select_finish);
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        allFoods = LitePal.findAll(AllFood.class);
        selectItemAdapter = new SelectItemAdapter(context, allFoods);

        setSupportActionBar(select_toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        select_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setListUi();
        select_finish.setOnClickListener(this);
    }

    //更新商品列表
    public void setListUi() {
        select_listview.setAdapter(selectItemAdapter);
    }

    //将用户所选的商品的总价格显示在右下角
    public void updataAllPrice() {
        select_allPrice.setText("一共:"+allallPrice+"元");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击"结算"按钮时,将用户点了的餐品的信息存放至数据库
            case R.id.select_finish:
                for (int i = 0; i < selectItemAdapter.getCount(); i++) {
                    //只有当餐品的数量不为0时才进行存储
                    if (selectItemAdapter.getItem(i).getNum() != 0) {
                        UserOrder userOrder = new UserOrder();
                        userOrder.setPhone(phone);
                        userOrder.setNum(selectItemAdapter.getItem(i).getNum());
                        userOrder.setOrderId(selectItemAdapter.getItem(i).getId());
                        userOrder.save();
                    }
                }
                //跳转至订单详情界面
                Intent intent = new Intent(this,OrderActivity.class);
                intent.putExtra("phone", phone);
                startActivity(intent);
                finish();
                break;
        }
    }
}
